package com.gxuwz.eschool;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gxuwz.eschool.fragment.CampusNewsFragment;
import com.gxuwz.eschool.fragment.NotificationAnnouncementFragment;

import java.util.Arrays;
import java.util.List;
//新闻公告
public class NewAnnouncementActivity extends AppCompatActivity {

    private LinearLayout ll_notification_announcement;
    private LinearLayout ll_campus_news;
    private FragmentTransaction fragmentTransaction;
    private NotificationAnnouncementFragment notificationAnnouncementFragment;
    private CampusNewsFragment campusNewsFragment;
    private TextView tv_notification_announcement;
    private TextView tv_campus_news;
    private View vw_notification_announcement;
    private View vw_campus_news;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_announcement);
        //初始话控件
        ll_notification_announcement = findViewById(R.id.ll_notification_announcement);
        tv_notification_announcement = findViewById(R.id.tv_notification_announcement);
        vw_notification_announcement = findViewById(R.id.vw_notification_announcement);
        ll_campus_news = findViewById(R.id.ll_campus_news);
        tv_campus_news = findViewById(R.id.tv_campus_news);
        vw_campus_news = findViewById(R.id.vw_campus_news);

        // 为每个LinearLayout设置点击监听器
        ll_notification_announcement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setFragment(notificationAnnouncementFragment,ll_notification_announcement);
            }
        });
        ll_campus_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setFragment(campusNewsFragment,ll_campus_news);
            }
        });
        // 创建Fragment
        notificationAnnouncementFragment = new NotificationAnnouncementFragment();
        campusNewsFragment = new CampusNewsFragment();
        setFragment(notificationAnnouncementFragment,ll_notification_announcement);

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                // 检查是否有Fragment可以回退
                if (fragmentManager.getBackStackEntryCount() > 0) {
                    fragmentManager.popBackStack(); // 返回上一个Fragment
                } else {
                    // 如果没有Fragment可以回退，可以结束当前Activity
                    finish();
                }
            }
        });
    }
        private void setFragment(Fragment fragment, LinearLayout selected) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_container, fragment);
        fragmentTransaction.commit();
        updateTextViewIcons(selected);

    }

    private void updateTextViewIcons(LinearLayout selected) {
        int defaultColor = Color.GRAY; // 默认颜色
        int selectedTextColor = Color.RED; // 选中文本颜色（示例）

        // 根据selected决定哪个TextView变为红色
        if (selected == ll_notification_announcement) {
            tv_notification_announcement.setTextColor(selectedTextColor);
            vw_notification_announcement.setBackgroundColor(selectedTextColor);
            tv_campus_news.setTextColor(defaultColor);
            vw_campus_news.setBackgroundColor(defaultColor);
        } else if (selected == ll_campus_news) {
            tv_campus_news.setTextColor(selectedTextColor);
            vw_campus_news.setBackgroundColor(selectedTextColor);
            tv_notification_announcement.setTextColor(defaultColor);
            vw_notification_announcement.setBackgroundColor(defaultColor);

        }
    }
}